package problem.nc052;

import java.util.Stack;

public class Solution {
    public boolean isValid (String s) {
        char[] chars = s.toCharArray();

        Stack<Character> stack = new Stack<>();

        for(char c : chars){
            if(c == '(' || c == '[' ||c == '{'){
                stack.push(c);
            }

            if(c == ')' || c == ']' ||c == '}'){
                if(stack.isEmpty()){
                    return false;
                }
                char pop = charswitch(stack.pop());
                if(pop != c){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
    public char charswitch(char c){
        switch (c){
            case '(':return ')';
            case '[':return ']';
            case '{':return '}';
            default:return '0';
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        String str = "()";
        System.out.println(s.isValid(str));
    }
}
